package com.gqz.patrol.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.patrol.app.entity.EnWzJfEquipEntity;
import com.gqz.patrol.app.mapper.PatorlEnWzJfEquipMapper;
import com.gqz.patrol.app.mapstruct.DefectEnWzJfEquipMapstruct;
import com.gqz.patrol.app.service.IPatorlEnWzJfEquipService;
import com.gqz.patrol.app.utils.QueryParamTool;
import com.gqz.patrol.vo.EnWzJfEquipVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 新能源设备信息 ҵ���߼�ʵ�ֲ�ServiceImpl
 */
@Service
@Slf4j
public class PatorlEnWzJfEquipServiceImpl extends ServiceImpl<PatorlEnWzJfEquipMapper, EnWzJfEquipEntity> implements IPatorlEnWzJfEquipService {

    @Qualifier("defectEnWzJfEquipMapstructImpl")
    @Autowired
    private DefectEnWzJfEquipMapstruct mapstruct;

    @Override
    public List<EnWzJfEquipVo> getById(String id) {
        MPJLambdaWrapper<EnWzJfEquipEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(EnWzJfEquipEntity::getSbbm, id);
        queryWrapper.selectAll(EnWzJfEquipEntity.class);
        List<EnWzJfEquipEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<EnWzJfEquipVo> page(RequestForm<EnWzJfEquipVo> requestForm) {
        EnWzJfEquipEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnWzJfEquipEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.clear();
        queryWrapper.selectAll(EnWzJfEquipEntity.class);
        queryWrapper.allEq(QueryParamTool.getsFunctionObjectMap(entity.getClass(), entity), false);
        Page<EnWzJfEquipEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<EnWzJfEquipEntity> iPage = baseMapper.selectJoinPage(page, EnWzJfEquipEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<EnWzJfEquipVo> requestForm) {
        EnWzJfEquipEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnWzJfEquipEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.select(EnWzJfEquipEntity::getSbbm);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<EnWzJfEquipVo> forms) {
        List<EnWzJfEquipEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnWzJfEquipEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<EnWzJfEquipVo> forms) {
        List<EnWzJfEquipEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnWzJfEquipEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }


    /**
     * 根据机房ID查找设备信息
     *
     * @param jfCodes
     * @return
     */
    @Override
    public List<EnWzJfEquipVo> getByJfCodes(String jfCodes) {
        LambdaQueryWrapper<EnWzJfEquipEntity> wrapper = Wrappers
                .lambdaQuery(EnWzJfEquipEntity.class)
                .eq(EnWzJfEquipEntity::getSsjfbm, jfCodes);
        List<EnWzJfEquipEntity> jfEquips = this.list(wrapper);
        return mapstruct.toVos(jfEquips);
    }
}
